using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ctsu.Admin.Forms.CoreLab;

using Syncfusion.Windows.Forms.Grid;
using Syncfusion.Windows.Forms.Tools;
using ctsu.Admin.Forms;
using ctsu.BaseForms;
using ctsu.Security;
using ctsu.Shared;
using ctsu.Data;


namespace ctsu.Admin.BusLogic.CoreLab
{

	public class BLFormLegacyLabels : BLCoreLabFormBaseImpl
	{
		private SelectLocationDlg _sld;
		#region control references
		private TextBox _id;
		private DateTimePickerAdv _batchInDate;

		private TextBox _patient;
		private TextBox _protocol;
		private TextBox _visit;
		private DateTimePickerAdv _visitDate;
		private TextBox _visitTime;
		private ComboBox _sampleType;
		private ComboBox _testType;
		private ComboBox _unitOfMeasure;
		private TextBox _labelsNum;
		private TextBox _smallNote;
		private TextBox _note;
		private CheckBox _isLegacy;

		private ComboBox _freezer;
		private NumericUpDown _shelf;
		private NumericUpDown _rack;
		private NumericUpDown _box;

		private Button _btnPatient;
		private Button _btnProtocol;
		private Button _btnNewPatient;
		private Button _btnNewSample;
		private Button _btnNewTest;
		private Button _btnNewUnit;
		private Button _btnAddBatch;
		private Button _btnEditBatch;
		private Button _btnClear;
		private Button _btnRemoveBatch;
		private Button _btnPrint;
		private Button _selectLocation;

		private CheckBox _chkShowPatient;
		private CheckBox _chkKeepSame;
		private CheckBox _chkPrintLabels;
		#endregion control references

		public BLFormLegacyLabels(FormMain formMain, FormBase form, FormInfoBase formInfo, DataTable tableSchema, Permission formSecurity, Hashtable configData)
			: base(formMain, form, formInfo, tableSchema, formSecurity, configData)
		{

		}

		#region initialize
		protected override void _vcManager_OnAfterHookEvents()
		{
			_id = (TextBox)Forms.Utils.GetControlByName("ID", this._form.Controls);
			_batchInDate = (DateTimePickerAdv)Forms.Utils.GetControlByName("BatchInDate", this._form.Controls);
			_protocol = (TextBox)Forms.Utils.GetControlByName("Protocol", this._form.Controls);
			_patient = (TextBox)Forms.Utils.GetControlByName("Patient", this._form.Controls);
			_visit = (TextBox)Forms.Utils.GetControlByName("Visit", this._form.Controls);
			_visitDate = (DateTimePickerAdv)Forms.Utils.GetControlByName("VisitDate", this._form.Controls);
			_visitTime = (TextBox)Forms.Utils.GetControlByName("VisitTime", this._form.Controls);
			_isLegacy = (CheckBox)Forms.Utils.GetControlByName("IsLegacy", this._form.Controls);
			_freezer = (ComboBox)Forms.Utils.GetControlByName("Freezer", this._form.Controls);
			_shelf = (NumericUpDown)Forms.Utils.GetControlByName("Shelf", this._form.Controls);
			_rack = (NumericUpDown)Forms.Utils.GetControlByName("Rack", this._form.Controls);
			_box = (NumericUpDown)Forms.Utils.GetControlByName("Box", this._form.Controls);

			_sampleType = (ComboBox)Forms.Utils.GetControlByName("SampleType", this._form.Controls);
			_testType = (ComboBox)Forms.Utils.GetControlByName("TestType", this._form.Controls);
			_unitOfMeasure = (ComboBox)Forms.Utils.GetControlByName("UnitOfMeasure", this._form.Controls);
			_labelsNum = (TextBox)Forms.Utils.GetControlByName("LabelsNum", this._form.Controls);
			_smallNote = (TextBox)Forms.Utils.GetControlByName("SmallNote", this._form.Controls);
			_note = (TextBox)Forms.Utils.GetControlByName("Note", this._form.Controls);

			_btnPatient = (Button)Forms.Utils.GetControlByName("btnPatient", this._form.Controls);
			_btnProtocol = (Button)Forms.Utils.GetControlByName("btnProtocol", this._form.Controls);
			_btnNewPatient = (Button)Forms.Utils.GetControlByName("btnNewPatient", this._form.Controls);
			_btnNewSample = (Button)Forms.Utils.GetControlByName("btnNewSample", this._form.Controls);
			_btnNewTest = (Button)Forms.Utils.GetControlByName("btnNewTest", this._form.Controls);
			_btnNewUnit = (Button)Forms.Utils.GetControlByName("btnNewUnit", this._form.Controls);

			_btnAddBatch = (Button)Forms.Utils.GetControlByName("btnAddBatch", this._form.Controls);
			_btnEditBatch = (Button)Forms.Utils.GetControlByName("btnEditBatch", this._form.Controls);
			_btnClear = (Button)Forms.Utils.GetControlByName("btnClear", this._form.Controls);
			_btnRemoveBatch = (Button)Forms.Utils.GetControlByName("btnRemoveBatch", this._form.Controls);
			_selectLocation = (Button)Forms.Utils.GetControlByName("SelectLocation", this._form.Controls);

			_chkShowPatient = (CheckBox)Forms.Utils.GetControlByName("chkShowPatient", this._form.Controls);
			_chkKeepSame = (CheckBox)Forms.Utils.GetControlByName("chkKeepSame", this._form.Controls);

			_btnPrint = (Button)Forms.Utils.GetControlByName("btnPrint", this._form.Controls);
			_chkPrintLabels = (CheckBox)Forms.Utils.GetControlByName("chkPrintLabels", this._form.Controls);
			_chkPrintReport = (CheckBox)Forms.Utils.GetControlByName("chkPrintReport", this._form.Controls);
			_chkPerPatient = (CheckBox)Forms.Utils.GetControlByName("chkPerPatient", this._form.Controls);
			_chkPrintSelected = (CheckBox)Forms.Utils.GetControlByName("chkPrintSelected", this._form.Controls);

			Button btnRemove = (Button)Forms.Utils.GetControlByName("btnRemove", this._form.Controls);
			btnRemove.Visible = false;
		}

		protected override void SetControlsEnabled(bool enable)
		{
			base.SetControlsEnabled(enable);

			_btnPatient.Enabled = enable;
			_btnProtocol.Enabled = enable;
			_btnNewPatient.Enabled = enable;
			_btnNewSample.Enabled = enable;
			_btnNewTest.Enabled = enable;
			_btnNewUnit.Enabled = enable;
			_btnAddBatch.Enabled = enable; ;
			_btnClear.Enabled = enable;
			_selectLocation.Enabled = enable;

			if (enable == false)
			{
				_btnEditBatch.Enabled = enable;
				_btnRemoveBatch.Enabled = enable;
			}

			_freezer.Enabled = enable;
			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
			_shelf.Enabled = enable;
			_shelf.Value = 0;
			_rack.Enabled = enable;
			_rack.Value = 0;
			_box.Enabled = enable;
			_box.Value = 0;
			_patient.Enabled = enable;
			_patient.Text = "";
			_protocol.Enabled = enable;
			_protocol.Text = "";
			_visitDate.Enabled = enable;
			_visitDate.Value = DateTime.Today;
			_visitDate.IsNullDate = true;
			_visit.Enabled = enable;
			_visit.Text = "";
			_visitTime.Enabled = enable;
			_visitTime.Text = "";
			_labelsNum.Enabled = enable;
			_labelsNum.Text = "";
			_smallNote.Enabled = enable;
			_smallNote.Text = "";
			_note.Enabled = enable;
			_note.Text = "";
			_chkShowPatient.Enabled = enable;
			_chkShowPatient.Checked = false;
			_chkKeepSame.Enabled = enable;
			_chkKeepSame.Checked = true;
			_sampleType.SelectedIndex = -1;
			_sampleType.SelectedIndex = -1;
			_sampleType.Enabled = enable;
			_testType.SelectedIndex = -1;
			_testType.SelectedIndex = -1;
			_testType.Enabled = enable;
			_unitOfMeasure.SelectedIndex = -1;
			_unitOfMeasure.SelectedIndex = -1;
			_unitOfMeasure.Enabled = enable;
		}

		public override void InitializeEmptyControls()
		{
			base.InitializeEmptyControls();
			_visitDate.Value = DateTime.Today;
			_visitDate.IsNullDate = true;
			_sampleType.SelectedIndex = -1;
			_sampleType.SelectedIndex = -1;
			_testType.SelectedIndex = -1;
			_testType.SelectedIndex = -1;
			_unitOfMeasure.SelectedIndex = -1;
			_unitOfMeasure.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
		}
		#endregion initialize

		public override void GenericClick(Control sender)
		{
			if (sender.Name == "btnPatient")
				SelectPatient();
			if (sender.Name == "btnProtocol")
				SelectProtocol();
			if (sender.Name == "btnNewPatient")
				DisplayPatientForm();
			if (sender.Name == "btnAddBatch")
				AddToBatch();
			if (sender.Name == "btnEditBatch")
				EditBatch();
			if (sender.Name == "btnRemoveBatch")
				RemoveBatch();
			if (sender.Name == "btnNewSample")
				NewSample();
			if (sender.Name == "btnNewTest")
				NewTest();
			if (sender.Name == "btnNewUnit")
				NewUnit();
			if (sender.Name == "btnClear")
				ClearInfo();
			if (sender.Name == "btnPrint")
				DoPrint();
			if (sender.Name == "SelectLocation")
				SelectLocation();
		}

		public override void New()
		{
			base.New();
			//check IsLegacy
			this._isLegacy.Checked = true;

		}

		#region select location
		public override void Cancel()
		{
			if (_sld != null)
				_sld.Close();
			base.Cancel();
		}

		private void SelectLocation()
		{
			_selectLocation.Enabled = false;
			int protocol = 0;
			string test = "";
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);
			GridRangeInfoList list;

			list = grid.Selections.GetSelectedRows(true, false);
			if (list.Count > 0)
			{
				GridRangeInfo range = list[0];
				int row = range.Top;

				protocol = (int)grid[row, 3].CellValue;
				test = grid[row, 6].FormattedText;
			}

			_sld = new SelectLocationDlg(test, protocol, grid, this._form.Text);
			_sld.Owner = this._form;
			_sld.SelectLocationEvent += new SelectLocationDlg.SelectLocationEventHandler(sld_SelectLocationEvent);
			_sld.Closing += new CancelEventHandler(sld_Closing);
			_sld.Show();
		}

		private void sld_SelectLocationEvent(object sender, SelectLocationEventArgs e)
		{
			DisplayLocationInControls(e.Location);
		}

		private void sld_Closing(object sender, CancelEventArgs e)
		{
			SelectLocationDlg sld = (SelectLocationDlg)sender;
			sld.SelectLocationEvent -= new SelectLocationDlg.SelectLocationEventHandler(sld_SelectLocationEvent);
			sld.Closing -= new CancelEventHandler(sld_Closing);
			if (this._formMode == EditMode.Edit || this._formMode == EditMode.New)
				this._selectLocation.Enabled = true;
			_sld = null;
		}

		#endregion select location

		#region search
		public override void Search(object sender)
		{
			Control ctl = (Control)sender;
			ctl.Enabled = false;

			SearchSamples spd = new SearchSamples();
			spd.ShowDialog(this._form);
			if (spd.SearchChoice == 0)
				base.Search(sender);
			else if (spd.SearchChoice == 1)
				SelectByLabel();

			ctl.Enabled = true;
		}

		private void SelectByLabel()
		{

			Cursor.Current = Cursors.WaitCursor;

			SearchInfo si = (SearchInfo)this.FormInfo.SearchFormInfo["TestSamples"];
			if (si == null)
			{
				MessageBox.Show("Search criteria is not defined.", "Search Criteria Not Fount",
					MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			FormSearch fs = new FormSearch();
			//set the title bar
			//fs.Text = si.Name + " Search";

			//get the search fields			
			foreach (ReportColumn rc in si.SearchFields)
				fs.SearchFields.Items.Add(rc);

			//design list view for result fields			
			ListView lvw = fs.SearchResults;
			lvw.Columns.Clear();
			if (lvw != null)
			{
				foreach (ReportColumn rc in si.SearchResultCols)
				{
					ColumnHeader ch = new ColumnHeader();
					ch.Text = rc.DisplayName;
					ch.Width = -2;
					lvw.Columns.Add(ch);
				}
			}

			SearchSamplesController sc = new SearchSamplesController(fs, si);
			fs.Controller = sc;
			fs.Owner = this._form;
			sc.ItemSelected += new ctsu.BaseForms.BusinessLogicEvents.BLSelectedItemEventHandler(sc_ItemSelected);
			fs.Closing += new CancelEventHandler(fs_Closing);
			IsSearchVisible = true;
			fs.Show();
		}

		private void sc_ItemSelected(object sender, BLReturnIDEventArgs e)
		{
			if (e.ID > 0)
			{
				Console.WriteLine("Selected ID: " + e.ID.ToString());
				if (GetData(e.ID))
				{
					if (this.FormMode == EditMode.Load)
						SetFormMode(EditMode.Read);
				}
			}
		}

		private void fs_Closing(object sender, CancelEventArgs e)
		{
			IsSearchVisible = false;
			this._form.btnSearch.Enabled = true;
		}
		#endregion search

		#region get display data
		private void ClearInfo()
		{
			_patient.Text = "";
			_patient.Tag = null;
			_protocol.Text = "";
			_protocol.Tag = null;
			_visitDate.Value = DateTime.Today;
			_visitDate.IsNullDate = true;
			_labelsNum.Text = "";
			_visit.Text = "";
			_visitTime.Text = "";
			_smallNote.Text = "";
			_note.Text = "";
			_sampleType.SelectedIndex = -1;
			_sampleType.SelectedIndex = -1;
			_testType.SelectedIndex = -1;
			_testType.SelectedIndex = -1;
			_unitOfMeasure.SelectedIndex = -1;
			_unitOfMeasure.SelectedIndex = -1;

			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
			_shelf.Value = 0;
			_rack.Value = 0;
			_box.Value = 0;
		}
		public override bool GetData(int id)
		{
			StringBuilder sb = new StringBuilder();
			string message = "";

			//this works only if there is one key and it is numeric
			//and -1 is not a possible vaule
			string keyFieldName = (string)this._fcManager.TableKeysOrdered[0];
			string sql = "SELECT * FROM " + this._formInfo.TableName +
				" WHERE " + keyFieldName + " =" + id +
				" AND IsLegacy=1";

			//SqlConnection conn = LoginForm.GetSqlConnection();
			try
			{
				DataSet ds = DataAccess.GetDataSet(sql);
				DataTable dt = ds.Tables[0];
				if (dt.Rows.Count == 1)
				{
					_fcManager.DbTable = dt;
					_fcManager.CurrentRow = dt.Rows[0];
					this._formMode = Shared.EditMode.Load;
					DisplayData();

					BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
					if (!fgbl.GetData(id))
					{
						//TODO - need to do more here for exception							
						MessageBox.Show("GetGridData Exception");
					}

					this._currentPrimaryID = id;
					this.CarryNewClicked(this._chkCarry);

					//get tool extra info and tool tips for patient and protocol
					GridControl grid = fgbl.Grid;
					for (int i = 1; i <= grid.RowCount; i++)
					{
						//protocol 
						int protoID = (int)grid[i, 3].CellValue;
						ProtocolInfo pri = CoreLabUtils.GetProtocolInfo(protoID, out message);
						grid[i, 3].Tag = pri;
						grid[i, 3].CellTipText = pri.GetToolTip();

						//patient
						int patID = (int)grid[i, 4].CellValue;
						PatientInfo pi = CoreLabUtils.GetPatientInfo(patID, out message);
						grid[i, 4].Tag = pi;
						grid[i, 4].CellTipText = pi.GetToolTip();
					}
				}
				return true;
			}
			catch (Exception ex)
			{
				sb.Append("Error in retrieving data");
				sb.Append(Environment.NewLine);
				sb.Append(ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Get Data Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				this.ClearCarryData();
				return false;
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		protected override void ResetCoreLabGrids()
		{
			base.ResetCoreLabGrids();

			string message = "";
			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
			GridControl grid = fgbl.Grid;
			grid.IgnoreReadOnly = true;

			//get tool extra info and tool tips for patient and protocol			
			for (int i = 1; i <= grid.RowCount; i++)
			{
				//protocol 
				int protoID = (int)grid[i, 3].CellValue;
				ProtocolInfo pri = CoreLabUtils.GetProtocolInfo(protoID, out message);
				grid[i, 3].Tag = pri;
				grid[i, 3].CellTipText = pri.GetToolTip();

				//patient
				int patID = (int)grid[i, 4].CellValue;
				PatientInfo pi = CoreLabUtils.GetPatientInfo(patID, out message);
				grid[i, 4].Tag = pi;
				grid[i, 4].CellTipText = pi.GetToolTip();
			}
			grid.IgnoreReadOnly = false;
		}

		protected override void grd_CellDoubleClick(object sender, GridCellClickEventArgs e)
		{
			if (e.RowIndex < 1)
				return;
			if (this.FormMode == EditMode.Edit || this.FormMode == EditMode.New)
				FillControlsWithRowData(e.RowIndex);
		}

		private void FillControlsWithRowData(int row)
		{
			GridControl grid = ((BLCoreLabFormGridBaseImpl)this._gridBLs["LegacySamples"]).Grid;

			ProtocolInfo pri = new ProtocolInfo();
			ProtocolInfo pri2 = (ProtocolInfo)grid[row, 3].Tag;
			pri.ID = pri2.ID;
			//pri.SPID = pri2.SPID;
			pri.Number = pri2.Number;
			pri.Investigator = pri2.Investigator;
			this._protocol.Text = pri.ID.ToString();
			this._protocol.Tag = pri;
			this._form.tip.SetToolTip(_protocol, pri.GetToolTip());

			PatientInfo pi = new PatientInfo();
			PatientInfo pi2 = (PatientInfo)grid[row, 4].Tag;
			pi.ID = pi2.ID;
			pi.FirstName = pi2.FirstName;
			pi.LastName = pi2.LastName;
			pi.MRN = pi2.MRN;
			pi.OtherID = pi2.OtherID;
			this._patient.Text = pi.ID.ToString();
			this._patient.Tag = pi;
			this._form.tip.SetToolTip(_patient, pi.GetToolTip());

			this._testType.Text = grid[row, 6].FormattedText;
			this._unitOfMeasure.Text = grid[row, 7].FormattedText;

			//location			
			string sVal = grid[row, 8].Text;
			DisplayLocationInControls(sVal);

			this._visitDate.Value = (DateTime)grid[row, 11].CellValue;
			this._visit.Text = grid[row, 12].Text;
			this._visitTime.Text = grid[row, 13].Text;
			this._sampleType.Text = grid[row, 5].FormattedText;
			this._note.Text = grid[row, 9].Text;
			this._smallNote.Text = grid[row, 10].Text;
		}

		private void ClearLocation()
		{
			this._freezer.SelectedIndex = -1;
			this._freezer.SelectedIndex = -1;
			this._shelf.Value = 0;
			this._rack.Value = 0;
			this._box.Value = 0;
		}

		private void DisplayLocationInControls(string location)
		{
			if (location.Length == 0)
			{
				ClearLocation();
				return;
			}

			string[] a = location.Split(',');
			foreach (string s in a)
			{
				if (s.StartsWith("Fz"))
				{
					this._freezer.SelectedValue = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Sf"))
				{
					this._shelf.Value = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Rk"))
				{
					this._rack.Value = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Bx"))
				{
					this._box.Value = Convert.ToInt32(s.Substring(3));
				}
			}
		}
		#endregion get display data

		#region edit remove batch
		private void RemoveBatch()
		{
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);

			GridRangeInfoList list;
			list = grid.Selections.GetSelectedRows(true, false);
			if (list.Count == 0)
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["LegacySamples"];

			ArrayList alRows = new ArrayList();
			int row = 0;
			int top = 0;
			int bottom = 0;
			for (int i = 0; i < list.Count; i++)
			{
				top = list[i].Top;
				bottom = list[i].Bottom;
				if (bottom > top)
					GetSelectedRows(alRows, top, bottom);
				else
					alRows.Add(list[i].Top);
			}
			for (int i = 0; i < alRows.Count; i++)
			{
				row = (int)alRows[i];
				row -= i;
				DataRow dr = (DataRow)fgbl.Grid[row, 0].Tag;
				dr.RejectChanges();
				fgbl.RemoveRow(row);
			}
		}

		private void GetSelectedRows(ArrayList alRows, int top, int bottom)
		{
			int rows = bottom - top + 1;
			for (int i = 0; i < rows; i++)
				alRows.Add(top + i);
		}

		private void EditBatch()
		{
			this._btnEditBatch.Enabled = false;
			LegacyBatchEditDlg ebd = new LegacyBatchEditDlg();

			ebd.ButtonClick += new ctsu.Admin.Forms.CoreLab.LegacyBatchEditDlg.ButtonClickEventHandler(ebd_ButtonClick);
			ebd.Closing += new CancelEventHandler(ebd_Closing);
			ebd.Owner = this._form;
			ebd.Show();
		}

		private void ebd_ButtonClick(object sender, EditBatchEventArgs e)
		{
			if (e.ButtonName == "ReplaceSelected")
				EditReplaceSelected(e.ReplaceColumns, e.ColumnMatch);
			if (e.ButtonName == "ReplaceAll")
				EditReplaceAll(e.ReplaceColumns, e.ColumnMatch);

		}

		private void ebd_Closing(object sender, CancelEventArgs e)
		{
			LegacyBatchEditDlg ebd = (LegacyBatchEditDlg)sender;
			ebd.ButtonClick -= new LegacyBatchEditDlg.ButtonClickEventHandler(ebd_ButtonClick);
			ebd.Closing -= new CancelEventHandler(ebd_Closing);
			if (this._formMode == EditMode.Edit || this._formMode == EditMode.New)
				this._btnEditBatch.Enabled = true;
		}

		private void EditReplaceAll(bool[] replaceColumns, Hashtable columnMatch)
		{
			Sample samp = new Sample();
			if (!SetReplacementColumns(replaceColumns, ref samp))
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["LegacySamples"];

			for (int i = 1; i <= fgbl.Grid.RowCount; i++)
				fgbl.ReplaceRow(samp, i);

			this._form.btnSave.Enabled = true;
		}

		private void EditReplaceSelected(bool[] replaceColumns, Hashtable columnMatch)
		{
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);

			GridRangeInfoList list;
			list = grid.Selections.GetSelectedRows(true, false);

			Sample samp = new Sample();
			if (!SetReplacementColumns(replaceColumns, ref samp))
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["LegacySamples"];

			foreach (GridRangeInfo range in list)
			{
				for (int i = range.Top; i <= range.Bottom; i++)
				{
					fgbl.ReplaceRow(samp, i);
					this._form.btnSave.Enabled = true;
				}
			}
		}

		private bool SetReplacementColumns(bool[] replaceColumns, ref Sample samp)
		{
			StringBuilder sb = new StringBuilder();
			if (replaceColumns[0])
			{
				if (!VerifyAddToBatch(1))
					return false;
				samp.Protocol = Convert.ToInt32(_protocol.Text);
				samp.ProtocolInfo = (ProtocolInfo)_protocol.Tag;
				samp.PatientID = Convert.ToInt32(_patient.Text);
				samp.PatientInfo = (PatientInfo)_patient.Tag;
				samp.VisitDate = _visitDate.Value;
				samp.Visit = _visit.Text;
				samp.VisitTime = _visitTime.Text;
				samp.SampleType = (int)_sampleType.SelectedValue;
				samp.TestType = (int)_testType.SelectedValue;
				samp.UnitOfMeasure = (int)this._unitOfMeasure.SelectedValue;
				samp.SmallNote = _smallNote.Text;
				samp.Note = _note.Text;
				samp.Location = GetFormatatedLocation();
			}
			if (replaceColumns[1])
			{
				if (_protocol.Text.Length == 0)
				{
					sb.Append("You must enter the protocol!");
					MessageBox.Show(this._form, sb.ToString(), "Protocol", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_protocol.Focus();
					return false;
				}
				samp.Protocol = Convert.ToInt32(_protocol.Text);
			}

			if (replaceColumns[2])
			{
				if (_patient.Text.Length == 0)
				{
					sb.Append("You must enter the patient!");
					MessageBox.Show(this._form, sb.ToString(), "Patient", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_patient.Focus();
					return false;
				}
				samp.PatientID = Convert.ToInt32(_patient.Text);
			}

			if (replaceColumns[3])
			{
				if (_visitDate.IsNullDate)
				{
					sb.Append("You must enter the visit date!");
					MessageBox.Show(this._form, sb.ToString(), "Visit Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_visitDate.Focus();
					return false;
				}
				samp.VisitDate = _visitDate.Value;
			}

			if (replaceColumns[4])
			{
				if (_visit.Text.Length == 0)
				{
					sb.Append("You must enter the visit!");
					MessageBox.Show(this._form, sb.ToString(), "Visit", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_visit.Focus();
					return false;
				}
				samp.Visit = _visit.Text;
			}

			if (replaceColumns[5])
			{
				if (_visitTime.Text.Length == 0)
				{
					sb.Append("You must enter the visit time!");
					MessageBox.Show(this._form, sb.ToString(), "Visit Time", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_visitTime.Focus();
					return false;
				}
				samp.VisitTime = _visitTime.Text;
			}

			if (replaceColumns[6])
			{
				if (_sampleType.SelectedIndex == -1)
				{
					sb.Append("You must enter the sample type!");
					MessageBox.Show(this._form, sb.ToString(), "Sample Type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_sampleType.Focus();
					return false;
				}
				samp.SampleType = (int)_sampleType.SelectedValue;
			}

			if (replaceColumns[7])
			{
				if (_testType.SelectedIndex == -1)
				{
					sb.Append("You must enter the test type!");
					MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_testType.Focus();
					return false;
				}
				samp.TestType = (int)_testType.SelectedValue;
			}

			if (replaceColumns[8])
			{
				if (_unitOfMeasure.SelectedIndex == -1)
				{
					sb.Append("You must enter the units of measure!");
					MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_unitOfMeasure.Focus();
					return false;
				}
				samp.UnitOfMeasure = (int)this._unitOfMeasure.SelectedValue;
			}

			if (replaceColumns[9])
			{
				if (_smallNote.Text.Length == 0)
				{
					sb.Append("You must enter the label note!");
					MessageBox.Show(this._form, sb.ToString(), "Label Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_smallNote.Focus();
					return false;
				}
				samp.SmallNote = _smallNote.Text;
			}

			if (replaceColumns[10])
			{
				if (_note.Text.Length == 0)
				{
					sb.Append("You must enter the comment!");
					MessageBox.Show(this._form, sb.ToString(), "Comment", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_note.Focus();
					return false;
				}
				samp.Note = _note.Text;
			}
			if (replaceColumns[11])
			{
				if (!VerifyLocation())
				{
					MessageBox.Show(this._form, "The location is invalid.", "Invalid Location", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					return false;
				}
				samp.Location = this.GetFormatatedLocation();
			}
			return true;
		}
		#endregion edit remove batch

		#region add batch
		private void AddToBatch()
		{
			if (!VerifyAddToBatch(0))
				return;

			IBLCoreLabFormGridBase fgbl = (IBLCoreLabFormGridBase)this._gridBLs["LegacySamples"];
			int numLabels = Convert.ToInt32(_labelsNum.Text);

			Sample samp = null;
			for (int i = 0; i < numLabels; i++)
			{
				samp = new Sample();
				samp.Protocol = Convert.ToInt32(_protocol.Text);
				samp.ProtocolInfo = (ProtocolInfo)_protocol.Tag;
				samp.PatientID = Convert.ToInt32(_patient.Text);
				samp.PatientInfo = (PatientInfo)_patient.Tag;
				samp.SampleType = (int)_sampleType.SelectedValue;
				samp.TestType = (int)_testType.SelectedValue;
				samp.UnitOfMeasure = (int)this._unitOfMeasure.SelectedValue;
				samp.VisitDate = _visitDate.Value;
				samp.Location = this.GetFormatatedLocation();
				samp.VisitTime = _visitTime.Text;
				samp.Visit = _visit.Text;
				samp.SmallNote = _smallNote.Text;
				samp.Note = this._note.Text;
				fgbl.AddRow(samp, 0);
			}
		}

		private bool VerifyAddToBatch(int type)
		{
			StringBuilder sb = new StringBuilder();

			if (type == 0)
			{
				if (_labelsNum.Text.Length == 0)
				{
					sb.Append("You must enter the number of labels!");
					MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_labelsNum.Focus();
					return false;
				}
			}

			if (_protocol.Text.Length == 0)
			{
				sb.Append("You must enter the protocol!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_protocol.Focus();
				return false;
			}

			if (_patient.Text.Length == 0)
			{
				sb.Append("You must enter the patient!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_patient.Focus();
				return false;
			}

			if (_visitDate.IsNullDate)
			{
				sb.Append("You must enter the visit date!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_visitDate.Focus();
				return false;
			}

			if (_visit.Text.Length == 0)
			{
				sb.Append("You must enter the visit!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_visit.Focus();
				return false;
			}

			if (_visitTime.Text.Length == 0)
			{
				sb.Append("You must enter the visit time!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_visitTime.Focus();
				return false;
			}

			if (_sampleType.SelectedIndex == -1)
			{
				sb.Append("You must enter the sample type!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_sampleType.Focus();
				return false;
			}

			if (_testType.SelectedIndex == -1)
			{
				sb.Append("You must enter the test type!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_testType.Focus();
				return false;
			}
			if (_unitOfMeasure.SelectedIndex == -1)
			{
				sb.Append("You must enter the units of measure!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_unitOfMeasure.Focus();
				return false;
			}
			if (!VerifyLocation())
			{
				sb.Append("You have entered an invalid location!");
				MessageBox.Show(this._form, sb.ToString(), "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				_freezer.Focus();
				return false;
			}
			return true;
		}
		private bool VerifyLocation()
		{
			StringBuilder sb = new StringBuilder();
			//allow this for dna extraction
			if (_testType.SelectedIndex != -1)
			{
				if ((int)_testType.SelectedValue == 49)
				{
					if (_freezer.SelectedIndex != -1)
						return true;
				}
			}

			if (_freezer.SelectedIndex == -1)
				return false;
			if (_shelf.Value == 0)
				return false;
			if (this._shelf.Value > 9)
				return false;
			if (this._rack.Value > 9)
				return false;
			if (_box.Value == 0)
				return false;
			return true;
		}

		private string GetFormatatedLocation()
		{
			string location = "Fz:" + this._freezer.SelectedValue.ToString() + "," +
				"Sf:" + this._shelf.Value.ToString() + "," +
				"Rk:" + this._rack.Value.ToString() + "," +
				"Bx:" + this._box.Value.ToString();
			return location;
		}
		#endregion add batch

		#region select patient and protocol
		private void SelectProtocol()
		{
			SelectProtocolDlg spd = new SelectProtocolDlg();
			spd.ShowDialog(this._form);
			if (spd.SearchChoice == 0)
				SelectProtocolStandard();
			else if (spd.SearchChoice == 1)
				SelectProtocolByInvestigator();
		}

		private void SelectProtocolByInvestigator()
		{
			StringBuilder sb = new StringBuilder();
			Cursor.Current = Cursors.WaitCursor;

			SearchInfo si = (SearchInfo)this._formInfo.SearchFormInfo["Investigator"];

			try
			{
				FormSearchBase fs = (FormSearchBase)SearchUtils.GetSearchDialog(si);
				fs.ShowDialog(this._form);
				if (fs.DialogResult == DialogResult.OK)
				{
					int id = fs.SelectedID;
					GetProtocolData(id);
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not display protocol seach dialog!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Display Search Dialog Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void SelectProtocolStandard()
		{
			StringBuilder sb = new StringBuilder();
			Cursor.Current = Cursors.WaitCursor;

			SearchInfo si = (SearchInfo)this._formInfo.SearchFormInfo["Protocol"];

			try
			{
				FormSearchBase fs = (FormSearchBase)SearchUtils.GetSearchDialog(si);
				fs.ShowDialog(this._form);
				if (fs.DialogResult == DialogResult.OK)
				{
					Console.WriteLine("DialogResult.OK");
					int id = fs.SelectedID;
					GetProtocolData(id);
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not display protocol seach dialog!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Display Search Dialog Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void SelectPatient()
		{
			StringBuilder sb = new StringBuilder();
			Cursor.Current = Cursors.WaitCursor;

			SearchInfo si = (SearchInfo)this._formInfo.SearchFormInfo["Patient"];
			FormSearchBase fs = (FormSearchBase)SearchUtils.GetSearchDialog(si);

			fs.ShowDialog(this._form);

			if (fs.DialogResult == DialogResult.OK)
			{
				Console.WriteLine("DialogResult.OK");
				int id = fs.SelectedID;
				Console.WriteLine("Selected ID: " + id.ToString());
				GetPatientData(id);
			}
		}

		private void DisplayPatientForm()
		{
			StringBuilder sb = new StringBuilder();
			FormPatient fp = new FormPatient();

			//need to create instance for bl			
			FormInfoBase fi = this._formMain.GetFormInfo(1, true);

			//check for permission
			Permission permission = Forms.FormSecurity.GetFormAccess(fi, LoginForm.ApplicationSecurity.ApplicationUser);
			if (permission == Permission.AccessDenied)
			{
				MessageBox.Show(this._form, "You do not have access rights for form: " + fi.Name, "Access Denied");
				return;
			}

			DataTable dt = Forms.Utils.GetTableSchema(fi.TableName, null);
			Hashtable ht = this._formMain.GetFormsConfigUserOptions();

			BLFormPatientDialogImpl bl = new BLFormPatientDialogImpl(this._formMain, fp, fi, dt, permission, ht);

			//create controller
			FormBaseControllerImpl ctlr = new FormBaseControllerImpl(fp, bl);
			if (!bl.Initialize())
			{
				sb.Append("Could not load form!");
				sb.Append(Environment.NewLine);
				sb.Append(Constants.NotifySupport);
				MessageBox.Show(this._form, sb.ToString(), "Loading Form Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			fp.Controller = ctlr;

			//bl.FormIcon = Icon.FromHandle(((Bitmap)this.imgL1.Images[fi.ImageIndex]).GetHicon());
			bl.GridView += new BusinessLogicEvents.BLGridViewEventHandler(this._formMain.OnGridView);
			bl.TableChange += new ctsu.BaseForms.BusinessLogicEvents.BLTableChangeEventHandler(this._formMain.OnTableChange);

			fp.Activated += new EventHandler(fp_Activated);
			fp.ShowDialog(this._form);
			if (fp.DialogResult == DialogResult.OK)
			{
				this.GetPatientData(fp.CurrentPrimaryID);
				//reset the grid column comboBox
				BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
				GridControl grid = fgbl.Grid;

				GridStyleInfo gsi = grid.ColStyles[4];
				SpecialLookup sl = (SpecialLookup)this._formInfo.SpecialLookups["Patient"];
				Forms.Utils.LoadCbo(sl, gsi);
			}
		}

		private void GetProtocolData(int id)
		{
			string message = "";
			ProtocolInfo pi = CoreLabUtils.GetProtocolInfo(id, out message);
			this._protocol.Text = id.ToString();
			this._protocol.Tag = pi;
			this._form.tip.SetToolTip(_protocol, pi.GetToolTip());
		}

		private void GetPatientData(int id)
		{
			StringBuilder sb = new StringBuilder();
			PatientInfo pi = new PatientInfo();
			string message = "";

			//TODO - have GetPatientInfo throw exception so we don't need message
			pi = CoreLabUtils.GetPatientInfo(id, out message);
			if (pi.ID == -1)
			{
				MessageBox.Show(this._form, message, "Patient Information",
					MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}
			this._patient.Text = pi.ID.ToString();
			this._patient.Tag = pi;
			this._form.tip.SetToolTip(_patient, pi.GetToolTip());
		}

		private void fp_Activated(object sender, EventArgs e)
		{
			FormPatient fp = (FormPatient)sender;
			BLFormPatientDialogImpl bl = (BLFormPatientDialogImpl)fp.Controller.BusinessLogic;
			bl.DisplayData();
		}
		#endregion select patient and protocol

		#region new combo items
		private void NewSample()
		{
			StringBuilder sb = new StringBuilder();

			string sql = "SELECT Display " +
				" FROM SampleType_L " +
				" WHERE 1=2";

			DataTable tableSchema;
			try
			{
				tableSchema = Forms.Utils.GetTableSchema(null, sql);
				NewListRecordDlg nd = new NewListRecordDlg("Display", "SampleType_L", tableSchema);
				nd.ShowDialog(this._form);

				Forms.Utils.LoadCbo(_sampleType, "SampleType_L", true);
				_sampleType.SelectedValue = nd.NewID;

				//reset the grid column comboBox
				BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
				GridControl grid = fgbl.Grid;

				GridStyleInfo gsi = grid.ColStyles[5];
				SpecialLookup sl = (SpecialLookup)this._formInfo.SpecialLookups["SampleType"];
				Forms.Utils.LoadCbo(sl, gsi);
			}
			catch (Exception ex)
			{
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Error encountered while retrieving table schema for SampleType.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
		}

		private void NewTest()
		{
			StringBuilder sb = new StringBuilder();

			string sql = "SELECT Display " +
				" FROM TestType_L " +
				" WHERE 1=2";

			DataTable tableSchema;
			try
			{
				tableSchema = Forms.Utils.GetTableSchema(null, sql);
				NewListRecordDlg nd = new NewListRecordDlg("Display", "TestType_L", tableSchema);
				nd.ShowDialog(this._form);

				Forms.Utils.LoadCbo(_testType, "TestType_L", true);
				_testType.SelectedValue = nd.NewID;

				//reset the grid column comboBox
				BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
				GridControl grid = fgbl.Grid;

				GridStyleInfo gsi = grid.ColStyles[6];
				SpecialLookup sl = (SpecialLookup)this._formInfo.SpecialLookups["TestType"];
				Forms.Utils.LoadCbo(sl, gsi);
			}
			catch (Exception ex)
			{
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Error encountered while retrieving table schema for TestType.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
		}

		private void NewUnit()
		{
			StringBuilder sb = new StringBuilder();

			string sql = "SELECT Display " +
				" FROM UnitOfMeasure_L " +
				" WHERE 1=2";

			DataTable tableSchema;
			try
			{
				tableSchema = Forms.Utils.GetTableSchema(null, sql);
				NewListRecordDlg nd = new NewListRecordDlg("Display", "UnitOfMeasure_L", tableSchema);
				nd.ShowDialog(this._form);

				Forms.Utils.LoadCbo(_unitOfMeasure, "UnitOfMeasure_L", true);
				_unitOfMeasure.SelectedValue = nd.NewID;

				//reset the grid column comboBox
				BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["LegacySamples"];
				GridControl grid = fgbl.Grid;

				GridStyleInfo gsi = grid.ColStyles[7];
				SpecialLookup sl = (SpecialLookup)this._formInfo.SpecialLookups["UnitOfMeasure"];
				Forms.Utils.LoadCbo(sl, gsi);
			}
			catch (Exception ex)
			{
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Error encountered while retrieving table schema for UnitOfMeasure.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
		}
		#endregion new combo items

		#region print
		private void DoPrint()
		{
			if (!HasRows())
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("There are no labels to print.");
				sb.Append(Environment.NewLine);
				MessageBox.Show(this._form, sb.ToString(), "No Labels", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			if (HasRowNoID())
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("You must press save to give the new labels an ID.");
				sb.Append(Environment.NewLine);
				MessageBox.Show(this._form, sb.ToString(), "No Label ID", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			if (_chkPrintLabels.Checked)
				PrintLabels();

			if (_chkPrintReport.Checked)
				PrintReport(0);

			if (_chkPerPatient.Checked)
				PrintReport(1);
		}

		private void PrintLabels()
		{
			string message = "";
			StringBuilder sb = new StringBuilder();
			string fileName = System.Windows.Forms.Application.LocalUserAppDataPath + "\\" + CremaConstants.LegacyLabelsFileName;
			string formatFile = System.Windows.Forms.Application.StartupPath + "\\Bartender\\" + CremaConstants.LegacyLabelsFormatFile;

			Cursor.Current = Cursors.WaitCursor;
			if (CreateTextFile(out message))
			{
				try
				{
                    //System.Windows.Forms.Application.DoEvents();
                    //_btApp = new BarTender.ApplicationClass();
                    //_btApp.LicenseServer.Connect();
                    //if (!_btApp.LicenseServer.IsConnected)
                    //{
                    //    Exception exx = new Exception("Unable to connect to bartender server.");
                    //    ExceptionPolicy.HandleException(exx, "Exception Policy");

                    //    sb.Append("Unable to connect to the label server.");
                    //    sb.Append(Environment.NewLine);
                    //    sb.Append("You are not able to print labels at this time.  Please try again later.");
                    //    MessageBox.Show(this._form, sb.ToString(), "Server Not Connected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //    return;
                    //}

					//check which printer to use
					string computerName = System.Net.Dns.GetHostName().ToUpper();
					string printer = "";
					switch (computerName)
					{
						case "PVTWXPG30203":
							printer = @"\\arctic\end07zlb01";
							break;
						case "PVTWXPG34207":
							printer = @"\\arctic\end07zlb01";
							break;
						case "PVTWXPG28306":
							printer = @"\\arctic\end07zlb01";
							break;
						case "R09870":
							printer = @"\\arctic\end07zlb01";
							break;
						case "PVTWXPG35325":
							printer = @"\\arctic\pav06zlb01";
							break;
						case "PVTWXPG":
							printer = @"\\arctic\far08zlb09";
							break;
					}

                    //BarTender.Format btFmt = _btApp.Formats.Open(formatFile, true, printer);

                    //IEnumerator ien = btFmt.Databases.GetEnumerator();
                    //while (ien.MoveNext())
                    //{
                    //    BarTender.Database btDb = (BarTender.Database)ien.Current;
                    //    btDb.TextFile.FileName = fileName;
                    //}
                    //btFmt.PrintOut(true, true);
				}
				catch (Exception ex)
				{
					//ExceptionPolicy.HandleException(ex, "Exception Policy");
					sb.Append("You must have the bar code program, 'Bartender', installed on your computer.");
					sb.Append(Environment.NewLine);
					sb.Append("If Bartender is insatlled then a connection to Bartender server could not be made.");
					MessageBox.Show(this._form, sb.ToString(), "Bartender Server Failed To Start", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				finally
				{
                    //if (_btApp != null)
                    //{
                    //    _btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
                    //    _btApp = null;
                    //}
				}
			}
			else
			{
				sb.Append("Could not create text file template for Bartender Server.");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + message);
				MessageBox.Show(this._form, sb.ToString(), "Bartender Server Create Text File Template Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private bool CreateTextFile(out string message)
		{
			StringBuilder sb = new StringBuilder();
			message = "";
			StreamWriter sw = null;
			string fileName = System.Windows.Forms.Application.LocalUserAppDataPath + "\\" + CremaConstants.LegacyLabelsFileName;

			bool doClear = false;
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);

			string test = "";
			int len = 0;
			try
			{
				int iVal = 0;
				string sVal = "";
				ProtocolInfo pri = null;
				PatientInfo pi = null;
				DateTime dtVal = new DateTime(0);
				sw = new StreamWriter(fileName, false);

				if (this._chkPrintSelected.Checked)
				{
					if (grid.Selections.Count == 0)
					{
						sb.Append("Select rows to print or uncheck the 'Print Select Rows' check box.");
						MessageBox.Show(this._form, sb.ToString(), "No Rows Selected", MessageBoxButtons.OK, MessageBoxIcon.Stop);
						return false;
					}
				}
				else
				{
					grid.Selections.Add(GridRangeInfo.Rows(1, grid.RowCount));
					doClear = true;
				}

				//write header
				sw.WriteLine(CremaConstants.LegacySampleHeader);

				GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
				for (int i = 0; i < list.Count; i++)
				{
					GridRangeInfo range = (GridRangeInfo)list[i];
					for (int row = range.Top; row <= range.Bottom; row++)
					{
						sb = new StringBuilder();
						//test label ID
						iVal = (int)grid[row, 1].CellValue;
						sb.Append(iVal.ToString("0000") + "~");

						//patient					
						iVal = Convert.ToInt32(grid[row, 4].CellValue);
						pi = (PatientInfo)grid[row, 4].Tag;
						if (_chkShowPatient.Checked)
						{
							if (pi.Name.Length > 0)
								sVal = pi.Name + "/" + pi.GetMrnOrOtherIdentifier();
							else
								sVal = pi.GetMrnOrOtherIdentifier();
						}
						else
							sVal = pi.GetMrnOrOtherIdentifier();
						sb.Append(sVal + "~");

						//spid
						//pri = (ProtocolInfo)grid[row, 3].Tag;
						//sVal = pri.SPID.ToString("0000");
						//sb.Append(sVal + "~");

						//test
						sVal = grid[row, 6].FormattedText;
						len = sVal.Length;
						if (len > 15)
							sVal = sVal.Substring(0, 14);
						test = sVal;

						//units
						sVal = grid[row, 7].FormattedText;
						if (sVal != "NA")
							test += " " + sVal;
						sb.Append(test + "~");

						//sample
						sVal = grid[row, 5].FormattedText;
						sb.Append(sVal + "~");

						//visit
						sVal = grid[row, 12].Text;
						sb.Append(sVal + "~");

						//Visit Date
						dtVal = (DateTime)grid[row, 11].CellValue;
						sb.Append(dtVal.ToShortDateString() + "~");

						//note
						sVal = grid[row, 10].Text;
						sb.Append(sVal + "~");

						sw.WriteLine(sb.ToString());
					}
				}
			}
			catch (Exception ex)
			{
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				message = ex.Message;
				return false;
			}
			finally
			{
				if (sw != null)
					sw.Close();
				if (doClear)
					grid.Selections.Clear();
			}
			return true;
		}


		private void PrintReport(int type)
		{
			StringBuilder sb = new StringBuilder();
			string formatFile = System.Windows.Forms.Application.StartupPath + "\\Bartender\\" + CremaConstants.LabelReportFormatFile;

			this._pDoc = new PrintDocument();
			this._pDoc.DefaultPageSettings.Landscape = true;
			this._pDlg = new PrintDialog();
			this._pDlg.Document = this._pDoc;

			if (this._pDlg.ShowDialog(this._form) == DialogResult.Cancel)
				return;

			bool doClear = false;
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);

			Cursor.Current = Cursors.WaitCursor;
			if (type == 0)
				this._pDoc.PrintPage += new PrintPageEventHandler(_pDoc_PrintPage);
			else
				this._pDoc.PrintPage += new PrintPageEventHandler(_pDoc_PrintPerPatient);

			_sBatch = this._currentPrimaryID.ToString();

			try
			{
				System.Windows.Forms.Application.DoEvents();
				//				_btApp = new BarTender.ApplicationClass();							
				//				BarTender.Format btFmt = _btApp.Formats.Open(formatFile, true, "");

				_page = 1;
				_startRow = 1;
				_startRange = 0;

				if (this._chkPrintSelected.Checked)
				{
					if (grid.Selections.Count == 0)
					{
						sb.Append("Select rows to print or uncheck the 'Print Select Rows' check box.");
						MessageBox.Show(this._form, sb.ToString(), "No Rows Selected", MessageBoxButtons.OK, MessageBoxIcon.Stop);
						return;
					}
					else
					{
						//get the start row								
						GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
						GridRangeInfo range = (GridRangeInfo)list[0];
						_startRow = range.Top;
					}
				}
				else
				{
					grid.Selections.Add(GridRangeInfo.Rows(1, grid.RowCount));
					doClear = true;
				}

				this._pDoc.Print();
			}
			catch (Exception ex)
			{
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Could not start bartender server");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Bartender Server Failed To Start", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				//				if(_btApp != null)
				//				{
				//					_btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
				//					_btApp = null;
				//				}

				if (type == 0)
					this._pDoc.PrintPage -= new PrintPageEventHandler(_pDoc_PrintPage);
				else
					this._pDoc.PrintPage -= new PrintPageEventHandler(_pDoc_PrintPerPatient);
				if (doClear)
					grid.Selections.Clear();
			}
		}

		private void _pDoc_PrintPage(object sender, PrintPageEventArgs e)
		{
			//string message = "";
			StringBuilder sb = new StringBuilder();
			Graphics gfx = e.Graphics;
			float pageBottom = gfx.VisibleClipBounds.Height;
			float pageWidth = gfx.VisibleClipBounds.Width;
			float leftMargin = 50;
			float topMargin = 50;

			Font prntFont = new Font("Arial", 10);
			Font titleFont = new Font("Arial", 14, FontStyle.Bold);
			Font subTitleFont = new Font("Arial", 12);
			Font ColumnHeadFont = new Font("Arial", 10, FontStyle.Bold);

			Pen pen = new Pen(Color.Black);

			//get the line height with some padding
			float lineHeight = prntFont.GetHeight(e.Graphics) + 2;

			//set the drawing to the top left of the page
			float yPos = topMargin;
			float xPos = leftMargin;

			//print page #
			string page = "Page " + this._page;
			PrintingUtils.PrintCenter(page, e, prntFont, pageBottom - lineHeight);

			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);
			string batchDate = ((Syncfusion.Windows.Forms.Tools.DateTimePickerAdv)Forms.Utils.GetControlByName("BatchInDate", this._form.Controls)).Value.ToShortDateString();

			//print title and subtitle
			if (this._page == 1)
			{
				PrintingUtils.PrintCenter("Core Lab - Legacy Labels Report", e, titleFont, yPos);
				yPos += titleFont.GetHeight(e.Graphics) + 2;

				sb.Append("Batch #: " + _sBatch);
				sb.Append("   Batch Date: " + batchDate);
				PrintingUtils.PrintCenter(sb.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			else
			{
				sb.Append("Legacy Labels Report - Batch #: " + _sBatch + " continued");
				PrintingUtils.PrintCenter(sb.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			yPos += lineHeight;

			//print heading
			float c1 = 25; //+125
			float c2 = 150; //+75			
			float c3 = 225; //100
			float c4 = 325; //100			
			float c5 = 425; //50
			float c6 = 475; //100
			float c7 = 575; //50			
			float c8 = 625; //100
			float c9 = 725; //150
			float c10 = 875;
			float c1W = 125; //id
			float c2W = 75;  //protocol
			float c3W = 100; //patient
			float c4W = 100; //test
			float c5W = 50; //unit
			float c6W = 100; //sample
			float c7W = 50; //visit
			float c8W = 100; //date
			float c9W = 150; //location
			float c10W = 150; //note

			RectangleF rec = new RectangleF();

			gfx.DrawRectangle(Pens.Black, 23, yPos, (pageWidth - 48), lineHeight);
			gfx.FillRectangle(Brushes.LightBlue, 23, yPos, pageWidth - 48, lineHeight);

			PrintingUtils.SetRectangle(ref rec, c1, yPos, lineHeight, c1W);
			gfx.DrawString("Test ID", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c2, yPos, lineHeight, c2W);
			gfx.DrawString("Protocol", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c3, yPos, lineHeight, c3W);
			gfx.DrawString("Patient", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c4, yPos, lineHeight, c4W);
			gfx.DrawString("Test Type", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c5, yPos, lineHeight, c5W);
			gfx.DrawString("Unit", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c6, yPos, lineHeight, c6W);
			gfx.DrawString("Sample Type", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c7, yPos, lineHeight, c7W);
			gfx.DrawString("Visit", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c8, yPos, lineHeight, c8W);
			gfx.DrawString("Date", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c9, yPos, lineHeight, c9W);
			gfx.DrawString("Location", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c10, yPos, lineHeight, c10W);
			gfx.DrawString("Comments", ColumnHeadFont, Brushes.Black, rec);
			yPos += lineHeight;

			lineHeight = prntFont.GetHeight(e.Graphics) + 18;
			//get the number of lines we can print
			int maxLines = (int)Math.Ceiling((double)((pageBottom - yPos) / lineHeight));
			maxLines -= 2;
			int curLine = 0;

			StringFormat fmt = new StringFormat();
			fmt.Trimming = StringTrimming.EllipsisCharacter;

			float yDraw;
			int iVal = 0;
			string sVal = "";
			PatientInfo pi = null;
			DateTime dtVal = new DateTime(0);

			GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
			for (int i = _startRange; i < list.Count; i++)
			{
				GridRangeInfo range = (GridRangeInfo)list[i];
				for (int row = range.Top; row <= range.Bottom; row++)
				{
					if (row < _startRow)
						row = _startRow;

					if (curLine == maxLines)
					{
						_startRow = row;
						_startRange = i;
						this._page++;
						e.HasMorePages = true;
						return;
					}

					yDraw = yPos + 5;
					//Label id
					iVal = (int)grid[row, 1].CellValue;
					//					_labelImage = CoreLabUtils.SetLabelFormatID(iVal.ToString("0000"), this._btApp);				
					//					gfx.DrawImage(this._labelImage, c1+5 , yDraw,c1W,lineHeight);
					PrintingUtils.SetRectangle(ref rec, c1, yPos, lineHeight, c1W);
					gfx.DrawString(iVal.ToString("0000"), prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c2, yPos, lineHeight, c2W);
					//protocol
					iVal = (int)grid[row, 3].CellValue;
					gfx.DrawString(iVal.ToString(), prntFont, Brushes.Black, rec);

					PrintingUtils.SetRectangle(ref rec, c3, yPos, lineHeight, c3W);
					//patient					
					iVal = (int)grid[row, 4].CellValue;
					pi = (PatientInfo)grid[row, 4].Tag;
					if (pi.Name.Length > 0)
						sVal = pi.Name + "/" + pi.GetMrnOrOtherIdentifier();
					else
						sVal = pi.GetMrnOrOtherIdentifier();
					gfx.DrawString(sVal, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c4, yPos, lineHeight, c4W);
					//test type
					gfx.DrawString(grid[row, 6].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c5, yPos, lineHeight, c5W);
					//unit
					gfx.DrawString(grid[row, 7].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c6, yPos, lineHeight, c6W);
					//sample
					gfx.DrawString(grid[row, 5].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c7, yPos, lineHeight, c7W);
					//visit
					gfx.DrawString(grid[row, 12].Text, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c8, yPos, lineHeight, c8W);
					//visitDate
					dtVal = (DateTime)grid[row, 11].CellValue;
					gfx.DrawString(dtVal.ToString("MM/dd/yyyy"), prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c9, yPos, lineHeight, c9W);
					//location
					gfx.DrawString(grid[row, 8].Text, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c10, yPos, lineHeight, c10W);
					//Comments
					gfx.DrawString(grid[row, 9].Text, prntFont, Brushes.Black, rec, fmt);

					gfx.DrawRectangle(Pens.Black, 23, yPos, (pageWidth - 48), lineHeight);

					yPos += lineHeight;
					curLine++;
				}
			}
			//gfx.DrawLine(Pens.Black, 23, yPos, (pageWidth - 48), yPos);
		}

		private void _pDoc_PrintPerPatient(object sender, PrintPageEventArgs e)
		{
			//string message = "";
			StringBuilder sb = new StringBuilder();
			Graphics gfx = e.Graphics;
			float pageBottom = gfx.VisibleClipBounds.Height;
			float pageWidth = gfx.VisibleClipBounds.Width;
			float leftMargin = 50;
			float topMargin = 50;

			Font prntFont = new Font("Arial", 10);
			Font titleFont = new Font("Arial", 14, FontStyle.Bold);
			Font subTitleFont = new Font("Arial", 12);
			Font ColumnHeadFont = new Font("Arial", 10, FontStyle.Bold);

			Pen pen = new Pen(Color.Black);

			//get the line height with some padding
			float lineHeight = prntFont.GetHeight(e.Graphics) + 2;

			//set the drawing to the top left of the page
			float yPos = topMargin;
			float xPos = leftMargin;

			//print page #
			string page = "Page " + this._page;
			PrintingUtils.PrintCenter(page, e, prntFont, pageBottom - lineHeight);

			GridControl grid = (GridControl)Forms.Utils.GetControlByName("LegacySamples", this._form.Controls);
			string batchDate = ((Syncfusion.Windows.Forms.Tools.DateTimePickerAdv)Forms.Utils.GetControlByName("BatchInDate", this._form.Controls)).Value.ToShortDateString();

			//print title and subtitle
			if (this._page == 1)
			{
				PrintingUtils.PrintCenter("Core Lab - Legacy Labels Report", e, titleFont, yPos);
				yPos += titleFont.GetHeight(e.Graphics) + 2;

				sb.Append("Batch #: " + _sBatch);
				sb.Append("   Batch Date: " + batchDate);
				PrintingUtils.PrintCenter(sb.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			else
			{
				sb.Append("Legacy Labels Report - Batch #: " + _sBatch + " continued");
				PrintingUtils.PrintCenter(sb.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			yPos += lineHeight;

			//print heading
			float c1 = 25; //+125
			float c2 = 150; //+75			
			float c3 = 225; //100
			float c4 = 325; //100			
			float c5 = 425; //50
			float c6 = 475; //100
			float c7 = 575; //50			
			float c8 = 625; //100
			float c9 = 725; //150
			float c10 = 875;
			float c1W = 125; //id
			float c2W = 75;  //protocol
			float c3W = 100; //patient
			float c4W = 100; //test
			float c5W = 50; //unit
			float c6W = 100; //sample
			float c7W = 50; //visit
			float c8W = 100; //date
			float c9W = 150; //location
			float c10W = 150; //note

			RectangleF rec = new RectangleF();

			gfx.DrawRectangle(Pens.Black, 23, yPos, (pageWidth - 48), lineHeight);
			gfx.FillRectangle(Brushes.LightBlue, 23, yPos, pageWidth - 48, lineHeight);

			PrintingUtils.SetRectangle(ref rec, c1, yPos, lineHeight, c1W);
			gfx.DrawString("Test ID", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c2, yPos, lineHeight, c2W);
			gfx.DrawString("Protocol", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c3, yPos, lineHeight, c3W);
			gfx.DrawString("Patient", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c4, yPos, lineHeight, c4W);
			gfx.DrawString("Test Type", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c5, yPos, lineHeight, c5W);
			gfx.DrawString("Unit", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c6, yPos, lineHeight, c6W);
			gfx.DrawString("Sample Type", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c7, yPos, lineHeight, c7W);
			gfx.DrawString("Visit", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c8, yPos, lineHeight, c8W);
			gfx.DrawString("Date", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c9, yPos, lineHeight, c9W);
			gfx.DrawString("Location", ColumnHeadFont, Brushes.Black, rec);
			PrintingUtils.SetRectangle(ref rec, c10, yPos, lineHeight, c10W);
			gfx.DrawString("Comments", ColumnHeadFont, Brushes.Black, rec);
			yPos += lineHeight;

			lineHeight = prntFont.GetHeight(e.Graphics) + 18;
			//get the number of lines we can print
			int maxLines = (int)Math.Ceiling((double)((pageBottom - yPos) / lineHeight));
			maxLines -= 2;
			int curLine = 0;

			StringFormat fmt = new StringFormat();
			fmt.Trimming = StringTrimming.EllipsisCharacter;

			float yDraw;
			int iVal = 0;
			//int patID = 0;
			string sVal = "";
			PatientInfo pi = null;
			DateTime dtVal = new DateTime(0);

			GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
			//assign current patient
			this._curPatient = (int)grid[_startRow, 4].CellValue;
			for (int i = _startRange; i < list.Count; i++)
			{
				GridRangeInfo range = (GridRangeInfo)list[i];
				for (int row = range.Top; row <= range.Bottom; row++)
				{
					if (row < _startRow)
						row = _startRow;

					pi = (PatientInfo)grid[row, 4].Tag;
					if (curLine == maxLines || this._curPatient != pi.ID)
					{
						_startRow = row;
						_startRange = i;
						_curPatient = (int)grid[row, 4].CellValue;
						this._page++;
						e.HasMorePages = true;
						return;
					}

					yDraw = yPos + 5;
					//Label id
					iVal = (int)grid[row, 1].CellValue;
					//					_labelImage = CoreLabUtils.SetLabelFormatID(iVal.ToString("0000"), this._btApp);				
					//					gfx.DrawImage(this._labelImage, c1+5 , yDraw,c1W,lineHeight);
					PrintingUtils.SetRectangle(ref rec, c1, yPos, lineHeight, c1W);
					gfx.DrawString(iVal.ToString("0000"), prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c2, yPos, lineHeight, c2W);
					//protocol
					iVal = (int)grid[row, 3].CellValue;
					gfx.DrawString(iVal.ToString(), prntFont, Brushes.Black, rec);

					PrintingUtils.SetRectangle(ref rec, c3, yPos, lineHeight, c3W);
					//patient																				
					if (pi.Name.Length > 0)
						sVal = pi.Name + "/" + pi.GetMrnOrOtherIdentifier();
					else
						sVal = pi.GetMrnOrOtherIdentifier();
					gfx.DrawString(sVal, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c4, yPos, lineHeight, c4W);
					//test type
					gfx.DrawString(grid[row, 6].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c5, yPos, lineHeight, c5W);
					//unit
					gfx.DrawString(grid[row, 7].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c6, yPos, lineHeight, c6W);
					//sample
					gfx.DrawString(grid[row, 5].FormattedText, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c7, yPos, lineHeight, c7W);
					//visit
					gfx.DrawString(grid[row, 12].Text, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c8, yPos, lineHeight, c8W);
					//visitDate
					dtVal = (DateTime)grid[row, 11].CellValue;
					gfx.DrawString(dtVal.ToString("MM/dd/yyyy"), prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c9, yPos, lineHeight, c9W);
					//location
					gfx.DrawString(grid[row, 8].Text, prntFont, Brushes.Black, rec, fmt);

					PrintingUtils.SetRectangle(ref rec, c10, yPos, lineHeight, c10W);
					//Comments
					gfx.DrawString(grid[row, 9].Text, prntFont, Brushes.Black, rec, fmt);

					gfx.DrawRectangle(Pens.Black, 23, yPos, (pageWidth - 48), lineHeight);

					yPos += lineHeight;
					curLine++;
				}
			}
			//gfx.DrawLine(Pens.Black, 23, yPos, (pageWidth - 48), yPos);
		}

		#endregion print

	}
}
